# Makefile for ping program

# Compiler
CC = gcc

# Compiler flags
CFLAGS = -Wall -Wextra -pedantic -std=c99

# Linker flags
LDFLAGS = -lpthread

# Target executable
TARGET = ping

# Source files
SRCS = ping.c

# Object files
OBJS = $(SRCS:.c=.o)

# Default target
all: $(TARGET)

# Compile target
$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)

# Compile source files to object files
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# Clean target
clean:
	rm -f $(OBJS) $(TARGET)

# Rebuild target
rebuild: clean all

# Phony targets
.PHONY: all clean rebuild

# Run with sudo (since raw sockets require root privileges)
run:
	sudo ./$(TARGET) 127.0.0.1

# Run with a specific IP address
run-ip:
	@if [ -z "$(IP)" ]; then \
		echo "Usage: make run-ip IP=<IP_ADDRESS>"; \
	else \
		sudo ./$(TARGET) $(IP); \
	fi